home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / chkbook / chkbkdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  9.8 KB  |  379 lines

  1. // ChkBkDoc.cpp : implementation of the CCheckBookDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chkbook.h"
  15.  
  16. #include "ChkBkDoc.h"
  17. #include "ChkBkVw.h"
  18. #include "mainfrm.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CCheckBookDoc
  28.  
  29. IMPLEMENT_DYNCREATE(CCheckBookDoc, CDocument)
  30.  
  31. BEGIN_MESSAGE_MAP(CCheckBookDoc, CDocument)
  32.     //{{AFX_MSG_MAP(CCheckBookDoc)
  33.     ON_COMMAND(ID_VIEW_BOOK, OnViewBook)
  34.     ON_COMMAND(ID_VIEW_CHECK, OnViewCheck)
  35.     ON_COMMAND(ID_EDIT_NEW_CHECK, OnEditNewCheck)
  36.     ON_COMMAND(ID_NEXT_CHECK, OnNextCheck)
  37.     ON_COMMAND(ID_PREV_CHECK, OnPrevCheck)
  38.     ON_COMMAND(ID_EDIT_COMMIT, OnEditCommit)
  39.     ON_COMMAND(ID_FILE_NEW, OnFileNew)
  40.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  41.     ON_COMMAND(ID_APP_EXIT, OnAppExit)
  42.     ON_UPDATE_COMMAND_UI(ID_NEXT_CHECK, OnUpdateNextCheck)
  43.     ON_UPDATE_COMMAND_UI(ID_PREV_CHECK, OnUpdatePrevCheck)
  44.     ON_UPDATE_COMMAND_UI(ID_VIEW_BOOK, OnUpdateViewBook)
  45.     ON_UPDATE_COMMAND_UI(ID_VIEW_CHECK, OnUpdateViewCheck)
  46.     //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CCheckBookDoc construction/destruction
  51.  
  52. CCheckBookDoc::CCheckBookDoc()
  53. {
  54.     m_bCheckView = TRUE;
  55.     m_nNewCheckNo = FIRST_CHECK_NO;
  56.     m_nCurrentCheckNo = m_nNewCheckNo;
  57.     m_CheckBook.Reset();
  58. }
  59.  
  60. CCheckBookDoc::~CCheckBookDoc()
  61. {
  62. }
  63.  
  64. //***********************************************************************
  65. // Function: CCheckBookDoc::OnNewDocument()
  66. //
  67. // Purpose:
  68. //    OnNewDocument customizes the process of opening a new
  69. //    new document.  This involves the setting the current 
  70. //    check number and new check number to FIRST_CHECK_NO (the
  71. //    first number in the check book) and setting the view to
  72. //    check view.
  73. //    UpdateMenu is then called which sets checks against the
  74. //    check view menu item and loads the check information into the
  75. //    view.
  76. //
  77. // Comments:
  78. //    In the debug version, an assert is generated if the index is
  79. //    out of bounds or if the item is disabled for the document.
  80. //
  81. //***********************************************************************
  82. BOOL CCheckBookDoc::OnNewDocument()
  83. {
  84.     // Initialize check information
  85. #if defined(_WIN32_WCE_PSPC)
  86.     FileOpenHelper(FALSE);
  87. #else
  88.     FileOpenHelper(TRUE);
  89. #endif
  90.         
  91.     return TRUE;
  92. }
  93.  
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CCheckBookDoc serialization
  96.  
  97. void CCheckBookDoc::Serialize(CArchive& ar)
  98. {
  99.     if (ar.IsStoring())
  100.     {
  101.     }
  102.     else
  103.     {
  104.     }
  105. }
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CCheckBookDoc diagnostics
  109.  
  110. #ifdef _DEBUG
  111. void CCheckBookDoc::AssertValid() const
  112. {
  113.     CDocument::AssertValid();
  114. }
  115.  
  116. void CCheckBookDoc::Dump(CDumpContext& dc) const
  117. {
  118.     CDocument::Dump(dc);
  119. }
  120. #endif //_DEBUG
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CCheckBookDoc helper functions
  124.  
  125. //***********************************************************************
  126. // Function: CCheckBookDoc::UpdateMenu()
  127. //
  128. // Purpose:
  129. //    UpdateMenu checks and enables menu items, and retrieves the values
  130. //    for the current check, passing on this information to the view.
  131. //    If the view is to check view, m_bCheckView is true.  Thus, the
  132. //    check view menu item is enabled and the bookv iew menu item is
  133. //    disabled.
  134. //    If the current check number is the last in the book, the Next Check
  135. //    menu item is disabled, and likewise with the Prev Check menu item,
  136. //    if the current check is the first in the book.
  137. //
  138. //***********************************************************************
  139.  
  140. void CCheckBookDoc::UpdateRecord() 
  141. {
  142.     // Enter the values for the current check
  143.     CCheckRecord* pChk = m_CheckBook.GetCheck(m_nCurrentCheckNo);
  144.     CString strPayTo, strDate, strMemo;
  145.     pChk->GetPayTo(strPayTo);
  146.     pChk->GetDate(strDate);
  147.     pChk->GetMemo(strMemo);
  148.  
  149.     // Pass check information to the view class
  150.     ((CCheckBookView*)m_pView)->SetCheckInfo(m_nCurrentCheckNo,
  151.         pChk->GetCents(), strPayTo, strDate, strMemo);
  152.     ((CCheckBookView*)m_pView)->CheckView(m_bCheckView);
  153. }
  154.  
  155. //***********************************************************************
  156. // Function: CCheckBookDoc::CheckThisCheck()
  157. //
  158. // Purpose:
  159. //    CheckThisCheck firstly checks to see if any changes have been made
  160. //    to the current check.  If this is the case, a message box is
  161. //    displayed (see QueryCommitCheck) asking the user if the check should
  162. //    be commited.  If the user clicks YES (returning the code IDYES), the
  163. //    check information is saved before continuing.
  164. //
  165. // Returns:
  166. //    If the user clicks YES or NO in the message box, the return value
  167. //    is IDOK.
  168. //    If the user clicks CANCEL, the return value is IDOK
  169. //
  170. //***********************************************************************
  171. UINT CCheckBookDoc::CheckThisCheck()
  172. {
  173.     if (!m_bCheckView)
  174.         return IDOK;
  175.     // Check to see if check has been altered
  176.     DWORD dwCents;
  177.     CString strPayTo, strDate, strMemo;
  178.     ((CCheckBookView*)m_pView)->GetCheckInfo(dwCents,
  179.         strPayTo, strDate, strMemo);
  180.  
  181.     if (!(m_CheckBook.IsCheckSame(m_nCurrentCheckNo, dwCents, 
  182.         strPayTo, strDate, strMemo)))
  183.     {
  184.         // If check has been altered, query if user wants to save
  185.         switch (((CCheckBookView*)m_pView)->QueryCommitCheck())
  186.         {
  187.         case IDCANCEL:
  188.             return IDCANCEL;
  189.         case IDNO:
  190.                 break;
  191.         default: // IDYES
  192.             {
  193.                 m_nNewCheckNo = m_CheckBook.SetCheck(m_nCurrentCheckNo,
  194.                     dwCents, strPayTo, strDate, strMemo);
  195.                 break;
  196.             }
  197.         }
  198.     }
  199.     return IDOK;
  200. }
  201.  
  202. CCheckBook* CCheckBookDoc::GetCheckBook()
  203. {
  204.     return &m_CheckBook;
  205. }
  206.  
  207. /////////////////////////////////////////////////////////////////////////////
  208. // CCheckBookDoc message handlers
  209.  
  210. void CCheckBookDoc::OnViewBook() 
  211. {
  212.     // If currently the check view,
  213.     // check if the check needs to be commited 
  214.     if (m_bCheckView && CheckThisCheck() == IDCANCEL)
  215.         return;    // Return if user changes mind
  216.  
  217.     m_bCheckView = FALSE;
  218.     UpdateRecord();
  219. }
  220.  
  221. void CCheckBookDoc::OnViewCheck() 
  222. {
  223.     // If currently the check view,
  224.     // check if the check needs to be commited 
  225.     if (m_bCheckView && CheckThisCheck() == IDCANCEL)
  226.         return; // Return if user changes mind
  227.  
  228.     m_bCheckView = TRUE;
  229.     UpdateRecord();
  230. }
  231.  
  232. void CCheckBookDoc::OnEditNewCheck() 
  233. {
  234.     // If currently the check view,
  235.     // check if the check needs to be commited 
  236.     if (m_bCheckView && CheckThisCheck() == IDCANCEL)
  237.         return; // Return if user changes mind
  238.  
  239.     m_nCurrentCheckNo = m_nNewCheckNo;    // Set the current check no to greatest
  240.     m_bCheckView = TRUE; // Put view into check view mode
  241.     UpdateRecord();
  242.     ((CCheckBookView*)m_pView)->NewCheck(m_nCurrentCheckNo);
  243. }
  244.  
  245. void CCheckBookDoc::OnNextCheck() 
  246. {
  247.     // Check that there is a next check in the book
  248.     if (m_nCurrentCheckNo >= m_nNewCheckNo - 1) 
  249.         return;
  250.  
  251.     if (CheckThisCheck() == IDCANCEL)
  252.         return;
  253.  
  254.     m_nCurrentCheckNo++;
  255.     UpdateRecord();
  256. }
  257.  
  258. void CCheckBookDoc::OnPrevCheck() 
  259. {
  260.     // Check that there is a previous check in the book
  261.     if (m_nCurrentCheckNo == FIRST_CHECK_NO)
  262.         return;
  263.  
  264.     if (CheckThisCheck() == IDCANCEL)
  265.         return;
  266.  
  267.     m_nCurrentCheckNo--;
  268.     UpdateRecord();
  269. }
  270.  
  271.  
  272. void CCheckBookDoc::OnEditCommit() 
  273. {
  274.     if (!m_bCheckView)
  275.         return;
  276.  
  277.     DWORD dwCents;
  278.     CString strPayTo, strDate, strMemo;
  279.     ((CCheckBookView*)m_pView)->GetCheckInfo(dwCents,
  280.         strPayTo, strDate, strMemo);
  281.  
  282.     if ((strPayTo == "") && (strDate == "") && (dwCents == 0))
  283.     {
  284.         m_pView->MessageBox(CString((LPCTSTR)IDS_MESSAGE2));
  285.         return;
  286.     }
  287.  
  288.     m_nNewCheckNo = m_CheckBook.SetCheck(m_nCurrentCheckNo,
  289.         dwCents, strPayTo, strDate, strMemo);
  290.     UpdateRecord();
  291. }
  292.  
  293. void CCheckBookDoc::OnFileNew() 
  294. {
  295.     if (CheckThisCheck() == IDCANCEL)
  296.         return;
  297.  
  298. #if defined(_WIN32_WCE_PSPC)
  299.     FileOpenHelper(FALSE);
  300. #else
  301.     FileOpenHelper(TRUE);
  302. #endif
  303. }
  304.  
  305. void CCheckBookDoc::OnFileOpen() 
  306. {
  307.     if (CheckThisCheck() == IDCANCEL)
  308.         return;
  309.  
  310.     FileOpenHelper(TRUE);
  311. }
  312.  
  313. void CCheckBookDoc::OnAppExit() 
  314. {
  315.     if (CheckThisCheck() == IDCANCEL)
  316.         return;
  317.  
  318.     ((CCheckBookApp*)AfxGetApp())->OnAppExit();
  319. }
  320.  
  321. void CCheckBookDoc::OnUpdateNextCheck(CCmdUI* pCmdUI) 
  322. {
  323.     // Disable Next Check menu item, if this is the last check in the book
  324.     pCmdUI->Enable(m_nCurrentCheckNo < (m_nNewCheckNo - 1));
  325. }
  326.  
  327. void CCheckBookDoc::OnUpdatePrevCheck(CCmdUI* pCmdUI) 
  328. {
  329.     // Disable Prev Check menu item, if this is the first check in the book
  330.     pCmdUI->Enable(m_nCurrentCheckNo != FIRST_CHECK_NO);
  331. }
  332.  
  333. void CCheckBookDoc::OnUpdateViewBook(CCmdUI* pCmdUI) 
  334. {
  335.     // Check the current view menu item
  336.     pCmdUI->SetCheck(!m_bCheckView);
  337. }
  338.  
  339. void CCheckBookDoc::OnUpdateViewCheck(CCmdUI* pCmdUI) 
  340. {
  341.     // Check the current view menu item
  342.     pCmdUI->SetCheck(m_bCheckView);
  343. }
  344.  
  345. void CCheckBookDoc::FileOpenHelper(BOOL bFileOpen) 
  346. {
  347.     m_bCheckView = TRUE;
  348.  
  349.     if (bFileOpen)
  350.     {
  351.         if (!m_CheckBook.Open())
  352.         {
  353.             if (!m_CheckBook.DoesBookExist())
  354.             {
  355.                 // Quit the program
  356.                 PostQuitMessage(0);
  357.             }
  358.             return;
  359.         }
  360.     }
  361.     else if (!m_CheckBook.New())
  362.     {
  363.         PostQuitMessage(0);
  364.         return;
  365.     }
  366.  
  367.     m_nNewCheckNo = m_CheckBook.GetNextNewCheckNo();
  368.     m_nCurrentCheckNo = FIRST_CHECK_NO;
  369.  
  370.     // Set the value for m_pView
  371.     POSITION pos = GetFirstViewPosition();
  372.     m_pView = GetNextView(pos);
  373.     ASSERT(pos == NULL);    // One and only one view
  374.     ASSERT(m_pView->IsKindOf(RUNTIME_CLASS(CCheckBookView)));
  375.  
  376.     // Update the information in the view and menu
  377.     UpdateRecord();
  378. }
  379.